package net.begincode.SSO.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import net.begincode.SSO.bean.SsoUser;
import net.begincode.SSO.bean.Url;
import net.begincode.SSO.enums.UserResponse;
import net.begincode.SSO.exception.UserException;
import net.begincode.SSO.mapper.UserMapper;
import net.begincode.SSO.util.JedisUtil;

/**
 * 用户业务层
 * @author 遗忘
 * @date 2017.4.17
 *
 */
@Service
public class UserService  {
	@Resource
	private UserMapper mapper;
	private Logger logger = LoggerFactory.getLogger(UserService.class);
	
	/**
	 * 用户注册，
	 * openId为空时代表普通注册，反之为第三方注册
	 * @param user
	 * @throws UserException 
	 */
	public int addUser(SsoUser user) throws UserException{
		if(user==null){
			logger.error("用户注册参数不能为空");
			throw new UserException(UserResponse.REGIST_USER_ERROR.getMessage());
		}
		if(user.getOpenId()!=null){
			return authThirdParty(user);
		}
		if(user.getEmail()==null||user.getPassword()==null){
			logger.error("用户注册参数不能为空");
			throw new UserException(UserResponse.REGIST_USER_ERROR.getMessage());
		}
		
		if(user.getEmail()!=null){
			if(findUserByEmail(user.getEmail())!=null){
				logger.error("该邮箱已被注册");
				throw new UserException(UserResponse.EMAILEXIST_ERROR.getMessage());
			}
		}
		user.setDeleteFlag(false);
		user.setRole(0);
		user.setVerify(false);
		mapper.addUser(user);
		return 1;
	}
	
	/**
	 * 通过邮箱查找用户
	 * @param email
	 * @return
	 */
	public SsoUser findUserByEmail(String email){
		return mapper.findUserByEmail(email);
	}
	
	/**
	 * 第三方登录保存信息
	 * @param user
	 */
	public int authThirdParty(SsoUser user){
		if(findUserByOpenId(user.getOpenId())!=null){
			logger.info("该用户已保存信息，无需再次保存");
			return 2;
		}
		user.setDeleteFlag(false);
		user.setVerify(false);
		user.setEmail("");
		user.setRole(0);
		user.setPassword("");
		mapper.addUser(user);
		return 2;
		
	}
	
	public SsoUser findUserByOpenId(String openId){
		return mapper.findUserByOpenId(openId);
	}
	
	/**
	 * 用户登录
	 * @param loginUser
	 * @throws UserException
	 */
	public SsoUser login(SsoUser loginUser) throws UserException{
		if(loginUser==null||loginUser.getEmail()==null){
			logger.error("登录的用户信息不能为空");
			throw new UserException(UserResponse.LOGIN_USER_ERROR.getMessage());
		}
		SsoUser user = mapper.findUserByEmail(loginUser.getEmail());
		if(user==null||!user.getPassword().equals(loginUser.getPassword())){
			logger.error("用户名或密码错误");
			throw new UserException(UserResponse.USERNAME_OR_PASSWORD_ERROR.getMessage());
		}else{
			return user;
		}
	}
	
	/**
	 * 模拟全局session 存储数据到redis中
	 * 使用UUID作为唯一标识
	 * @return
	 */
	public String intoRedis(){
		UUID uuid = UUID.randomUUID(); 
		
		String key = uuid.toString();
		List<String> list = new ArrayList<String>();
		list.add("none");
		JedisUtil.setList(key,list,60*30);
		
		return key;
	}
	
	/**
	 * 验证登录状态
	 * @param url
	 * @return
	 */
	public boolean verifyLoginState(Url url){
		if(JedisUtil.existsKey(url.getTicket())){
			JedisUtil.setList(url.getTicket(), url.getLogoutURL(), 60*30);
			return true;
		}else{
			return false;
		}
	}
	
}
